// const pageStack =  getCurrentPages()
const app = getApp()
// console.log(pageStack);
Page({
  data:{

    icon:'',
    username:'',
    mobile:'',
    birthday:'',
    sex:1,
    address:'',
    notes:'',
   
    mobile:'',
    dateLayerVisible:false,
    mobileLayerVisible:false,
    sexLayerVisible:false,
    homeLayerVisible:false,
    totesLayerVisible:false,
    columns: ['男','女'],
  },

  onLoad() {
    // 没有登录跳出！
    if (!app.token) return
    // 登录了，有用户资料，设置; 没有获取用户资料
    if (app.userProfile){
     
       console.log(app.userProfile);
      this.setData({...app.userProfile})
    } else {
      this.getUserInfo()
      console.log(1);
    }
  },
  // 获取用户信息


  updateName(ev){
     console.log(ev);
     this.setData({username:ev.detail.value})
     app.userProfile.username = ev.detail.value
  },





 async getUserInfo(){
   const {data:{userInfo:{address,birthday,icon,mobile,notes,username}}} = await wx.http.get('/user/getUserInfo')
  //  console.log(res);
  this.setData({address,birthday,icon,mobile,notes,username})
  app.userProfile = {address,birthday,icon,mobile,notes,username}
  },


  getUserAvatar(ev){
    // console.log(ev);
    this.updateAvatar(ev.detail.avatarUrl)
    // this.setData({icon:ev.detail.avatarUrl})
    console.log(ev.detail.avatarUrl);
  },

  updateAvatar(icon){
    console.log(icon);
    wx.uploadFile({
      //filePath:本地图片的资源路径
      filePath:icon,
      //后端接收文件的名称
      name:'file',
      //服务器的地址
      url:wx.http.baseURL + '/common/upload?type=images',
      header:{
        Token:getApp().token
      },
      success:(res) =>{
        const data = JSON.parse(res.data)
        console.log(data);
        // if(data.code !== 10000){
        //   return wx.utils.toast()
        // }
        // console.log(pageStack[0]);
        // pageStack[0].setData({icon:'http://124.223.14.236:8060/' + data.data.savePath})
        this.setData({icon: data.data.savePath})
        // // console.log(111);
        // // console.log(data.data.url);
        app.userProfile.icon =  data.data.savePath
        console.log(app.userProfile.icon);
        // this.setData({icon: app.userProfile.icon})

      }
    })
  },
// 留言
totesonClickLeft(){
  this.setData({ totesLayerVisible: false })
},
totesonClickRight(){
  this.setData({ totesLayerVisible: false })
},
notesInput(ev){
  this.setData({notes:ev.detail})
  app.userProfile.notes = ev.detail
},

  // 家庭


  homeonClickLeft(){
    this.setData({ homeLayerVisible: false })
  },
  homeonClickRight(ev){
    this.setData({ homeLayerVisible: false })
  },
  HomeInput(ev){
    // console.log(ev);
    this.setData({address:ev.detail})
    app.userProfile.address = ev.detail

  },
  // 生日
  confirmDateInfo(ev){
    this.setData({ dateLayerVisible: false })

    const dateString = ev.detail;
const date = new Date(dateString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2,'0');
const day = String(date.getDate()).padStart(2,'0');
const formattedDate = `${year}-${month}-${day}`;
    
    this.setData({birthday:formattedDate})
    app.userProfile.birthday = formattedDate
  },
  cancelDateLayer(){
    this.setData({ dateLayerVisible: false })
  },
  // 性别
  sexCancel(){
    this.setData({ sexLayerVisible: false })
  },
  sexConfirm(ev){
    console.log(ev);
   const sex = ev.detail === '男' ? 1 :0
     this.setData({sex})
    this.setData({ sexLayerVisible: false })
    app.userProfile.sex = sex
  },
  opensexLayer(){
    this.setData({ sexLayerVisible: true })
  },
  closesexLayer() {
    this.setData({ sexLayerVisible: false })
  },
  // 手机
  mobileonClickLeft() {
    this.setData({ mobileLayerVisible: false })
    wx.showToast({ title: '点击返回', icon: 'none' });
  },
  mobileonClickRight() {
    wx.showToast({ title: '确定', icon: 'none' })
    // this.handleInput()
    this.setData({ mobileLayerVisible: false })
  },
  handleInput(ev){
    console.log(ev);
    this.setData({mobile:ev.detail})
    app.userProfile.mobile = ev.detail
  },
  openMobileLayer(){
    this.setData({ mobileLayerVisible: true })
  },
  closeMobileLayer() {
    this.setData({ mobileLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  openHomeLayer() {
    this.setData({ homeLayerVisible: true })
  },
  closeHomeLayer() {
    this.setData({ homeLayerVisible: false })
  },
  opentotesLayer() {
    this.setData({ totesLayerVisible: true })
  },
  closetotesLayer() {
    this.setData({ totesLayerVisible: false })
  },

  chooseavatar(){

  }
})